/*
	DS Kanji Flash
	Copyright (C) 2009,2010 Bernhard Schelling

	This file is part of DS Kanji Flash.

	DS Kanji Flash is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	DS Kanji Flash is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with DS Kanji Flash.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "global.h"
#include "dict.h"

#define DRAWX ((256-128)/2)
#define DRAWY 10

static wchar_t wcGuess = 0;
static sRect rbtnBack(20, 170, 216, 8);
static const u8 *pStrokes = NULL;

class cSceneDraw : public cScene
{
public:
	cSceneDraw() : cScene(SCN_DRAW)
		{ WantsKeyHeldEvents = true; WantsKeyReleaseEvents = true; }
	
	virtual void Enter(void *p1, void *p2, void *p3)
	{
		pStrokes = GetCharStrokes(GetKanjiUCS((int)p1));
		cScene::Get(SCN_SHOWSTROKES)->Enter(p1);
		DrawClearImage();
	}

	virtual void ProcessInput()
	{
		//if (ul_keys.pressed.touch && rbtnBack.touchButton()) cScene::GoTo(SCN_CARD);
		if (ul_keys.pressed.touch && rbtnBack.touchButton()) wcGuess = GuessChar();
		DrawProcessInput();
	}

	virtual void DrawScreen_Top()
	{
		//cScene::Get(SCN_CARD)->DrawScreen_Top();
		cScene::Get(SCN_SHOWSTROKES)->DrawScreen_Top();
		//int dx = DRAWX, dy = (192-128)/2;
	}

	virtual void DrawScreen_Bottom()
	{
		ulDrawGradientRect(0, 0, 256, 192, RGB15(31, 31, 31), RGB15(31, 31, 31), RGB15(25, 25, 25), RGB15(25, 25, 25));

		DrawDisplay(pStrokes);

		u8 correction[DRAW_MAX_STROKES][JUDGE_PREC][2];
		float fScore = GetDrawScore(pStrokes, correction);
		for (u8 s = 0; s < pStrokes[0] && *(u32*)(void*)(correction[s]) == 0xFFFFFFFF; s++)
			for (int i = 0; i < JUDGE_PREC; i++)
				ulDrawFillRect(DRAWX+correction[s][i][0]-0, DRAWY+correction[s][i][1]-0, DRAWX+correction[s][i][0]+1, DRAWY+correction[s][i][1]+1, COL_RED);
				//ulDrawLine(DRAWX+mydpoints[s][i][0], DRAWY+mydpoints[s][i][1], DRAWXdx+refx, DRAWY+refy, COL_RED);

		char bla[100];
		sprintf(bla, "%f               ", fScore);
		//sprintf(bla, "d:%d, s:%d, pres:%d", cDrawn, pStrokes[0], bDrawing);
		drawButtonBG(rbtnBack, BUTTON_BG_BLUE, bla/*"Back"*/);
		char pcChar[4]; pcChar[SetUCS(pcChar, wcGuess)] = 0; displayStringCenter(fntJapanese, pcChar, rbtnBack);
	}
	
} SceneDraw;
